<?php defined('SYSPATH') OR die('No direct access allowed.');

class Admin_Controller extends Template_Controller {
	const ALLOW_PRODUCTION = FALSE;
	public $template = 'layouts/template_hongshaw_detail';

	public function index(){
		$this->template->content = new View('admin/login');
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->title = '';
	}

	public function doLogin(){
		$this->template->content = new View('admin/main_admin');
		$this->template->content->categorys=ORM::factory("category")->find_all();
		if ($_POST && Captcha::valid($this->input->post('captcha_response'))){
			$session = Session::instance();
			$username=$this->input->post("username");
			$password=$this->input->post("password");
			$this->template->title = '';
			$this->template->content->username=$username;
			$this->template->content->password=$password;
			$this->template->content->captcha_response=$this->input->post("captcha_response");
			$this->template->content->admin=false;
	
			if($password!='' && $username!=''){
				$this->template->content->user = ORM::factory("user")
				->where("username",$username)
				->where("password",md5($password))
				->where("role_id",1)
				->find();
				if(empty($this->template->content->user)){
					$session->set("admin",NULL);
				}else{
					$session->set("admin",$this->template->content->user);
					$this->template->content->admin=true;
				}
			}
	
			$this->template->content->password_auth=md5($this->input->post('password'));
		}else{
			$this->template->content = new View('admin/login');
			$this->template->content->captcha = Captcha::factory();
			$this->template->content->categorys=ORM::factory("category")->find_all();
		}

		
	}

	public function doLogout(){
		$this->template->content = new View('admin/login');
		$this->template->title = '';
		$this->template->content->captcha = Captcha::factory();
		$this->template->content->categorys=ORM::factory("category")->find_all();
		$this->template->title = '';
		$session = Session::instance();
		$session->set("admin",NULL);
	}



}
